﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StateManager
{
    class GenericMenu : GenericScreenCode
    {
        List<MenuItems> menuEntries = new List<MenuItems>();
        int selectedEntry = 0;
        string menuTitle;

        protected IList<MenuItems> MenuEntries
        {
            get { return menuEntries; }
        }

        public GenericMenu(string menuTitle)
        {
            this.menuTitle = menuTitle;

        }

        public override void HandleInput(Input input)
        {
            // Move to the previous menu entry
            if (input.IsMenuUp(ControllingPlayer))
            {
                selectedEntry--;

                if (selectedEntry < 0)
                    selectedEntry = menuEntries.Count - 1;
            }

            // Move to the next menu entry
            if (input.IsMenuDown(ControllingPlayer))
            {
                selectedEntry++; // the next menu entry is hovered over

                if (selectedEntry >= menuEntries.Count)
                    selectedEntry = 0; // if the menu entry is the last one and the player presses down again, then the topmost entry is hovered over
            }

            PlayerIndex playerIndex;

            if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
            {
                OnSelectEntry(selectedEntry, playerIndex);
            }
            else if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
            {
                OnCancel(playerIndex);
            }
        }

        protected virtual void UpdateMenuEntryLocations()
        {

            // start at Y = 175; each X value is generated per entry
            Vector2 position = new Vector2(0f, 300f);

            // update each menu entry's location in turn
            for (int i = 0; i < menuEntries.Count; i++)
            {
                MenuItems menuEntry = menuEntries[i];

                position.X = SManager.GraphicsDevice.Viewport.Width / 5 - menuEntry.GetWidth(this) / 2;


                

                // set the entry's position
                menuEntry.Position = position;

                // move down for the next entry the size of this entry
                position.Y += menuEntry.GetHeight(this);
            }
        }

        // this event switchs which menu entry is selected
        protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
        {
            menuEntries[entryIndex].OnSelectEntry(playerIndex); 
        }


        
        // Handler for when the user has cancelled the menu.   
        protected virtual void OnCancel(PlayerIndex playerIndex)
        {
            ExitScreen(); // if the event is to cancel, the screen is closed and control is given to the previous screen in the stack
        }


        /// <summary>
        /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
        /// </summary>
        protected void OnCancel(object sender, PlayerEventHandler e)
        {
            OnCancel(e.PlayerIndex);
        }

        public override void Update(GameTime gameTime, bool setFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, setFocus, coveredByOtherScreen);

            for (int i = 0; i < menuEntries.Count; i++)
            {
                bool isSelected = IsActive && (i == selectedEntry);
                menuEntries[i].Update(this, isSelected, gameTime);
                // update all of the other menus within the stack
            }
        }


        public override void Draw(GameTime gameTime)
        {
            // make sure our entries are in the right place before we draw them
            UpdateMenuEntryLocations();

            GraphicsDevice graphics = SManager.GraphicsDevice;
            SpriteBatch spriteBatch = SManager.SpriteBatch;
            SpriteFont font = SManager.SpriteFont;

            spriteBatch.Begin();

            // Draw each menu entry in turn.
            for (int i = 0; i < menuEntries.Count; i++)
            {
                MenuItems menuEntry = menuEntries[i];

                bool isSelected = IsActive && (i == selectedEntry);

                menuEntry.Draw(this, isSelected, gameTime);
            }


            // Draw the menu title centered on the screen
            Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 80);
            Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
            Color titleColor = new Color(192, 192, 192);
            float titleScale = 1.25f;

            titlePosition.Y -= 50;

            spriteBatch.DrawString(font, menuTitle, titlePosition, Color.Brown, 0,
                                   titleOrigin, titleScale, SpriteEffects.None, 0);

            spriteBatch.End();
        }
    }
}
